package cn.topevery.garden.dal.logic;

import cn.topevery.garden.dal.dao.GpsClientStateDao;
import cn.topevery.garden.entity.po.Extent;
import cn.topevery.garden.entity.po.GpsClientState;
import cn.topevery.garden.entity.query.GpsClientStateQuery;
import cn.topevery.garden.entity.vo.CarVO;
import cn.topevery.garden.entity.vo.GpsClientStateVO;
import cn.topevery.garden.entity.vo.PersonVO;
import cn.topevery.garden.framework.base.BaseLogic;
import cn.topevery.garden.framework.util.DateTimeUtil;
import hw.topevery.framework.GeometryHelper;
import hw.topevery.framework.db.base.BaseEntityDao;
import hw.topevery.framework.db.entity.DataPager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 状态详情
 *
 * @author code-generate
 * @date 2021-09-13
 */
@Service
public class GpsClientStateLogic extends BaseLogic<GpsClientState> {
    @Autowired
    GpsClientStateDao gpsClientStateDao;

    @Autowired
    private PersonLogic personLogic;

    @Autowired
    private CarLogic carLogic;

    @Override
    public BaseEntityDao<GpsClientState, Integer> getDao() {
        return gpsClientStateDao;
    }

    

    /**
     * 获取列表
     *
     * @param query
     */
    public DataPager<GpsClientStateVO> listPage(GpsClientStateQuery query) {
        if (query.getObjType() == 0) {
            PersonVO personVO = personLogic.getByUserId(query.getObjId());
            query.setId(personVO.getId());
            return gpsClientStateDao.listPage(personVO.getName(), query);
        } else {
            CarVO carVO = carLogic.getByCarId(query.getObjId());
            query.setId(carVO.getId());
            return gpsClientStateDao.listPage(carVO.getCarNum(), query);
        }
    }

    public List<Map<String, Object>> getObjCurrentPos(GpsClientStateQuery query) {
        return gpsClientStateDao.getObjCurrentPos(query);
    }

    public Extent getExtentByTypeAndId(GpsClientStateQuery query) {
        Extent extent = gpsClientStateDao.getExtentByTypeAndId(query);
        if ( extent != null && extent.getGeomBd09() != null) {
            extent.setMapRangesPoints(GeometryHelper.convertGeometryToPoint(extent.getGeomBd09()));
        }
        return extent;
    }

    public Map<String, Object> getGpsTrace(GpsClientStateQuery query) {
        List<List<Map<String, Object>>> list = gpsClientStateDao.getGpsTrace(query);

        Map<String, Object> res = new HashMap<>();
        res.put("Traces", list.get(1));
        res.put("Id", query.getObjId());
        if (list.get(2).size()>0){
            res.put("PlateNumber",list.get(2).get(0).get("c_name"));
            res.put("TotalDistance",list.get(2).get(0).get("c_total_distance"));
            res.put("TotalOilConsumption",list.get(2).get(0).get("c_total_oil_consumption"));
            res.put("TotalTime", DateTimeUtil.GetTimeString(Integer.parseInt(list.get(2).get(0).get("c_total_seconds").toString())));
        }
        if(list.get(1).size()>0){
            LocalDateTime s = (LocalDateTime) list.get(1).get(0).get("c_gps_date");
            LocalDateTime e = (LocalDateTime) list.get(1).get(list.get(1).size()-1).get("c_gps_date");
            Map<String, Object> btp = new HashMap<>();
            btp.put("GpsDate",s);
            btp.put("Long",list.get(1).get(0).get("c_long"));
            btp.put("Lat",list.get(1).get(0).get("c_lat"));
            btp.put("AbsX",list.get(1).get(0).get("c_abs_x"));
            btp.put("AbsY",list.get(1).get(0).get("c_abs_y"));
            res.put("btp",btp);
            Map<String, Object> etp = new HashMap<>();
            etp.put("GpsDate",e);
            etp.put("Long",list.get(1).get(list.get(1).size()-1).get("c_long"));
            etp.put("Lat",list.get(1).get(list.get(1).size()-1).get("c_lat"));
            etp.put("AbsX",list.get(1).get(list.get(1).size()-1).get("c_abs_x"));
            etp.put("AbsY",list.get(1).get(list.get(1).size()-1).get("c_abs_y"));
            res.put("etp",etp);

        }
        return res;
    }

    public  Map<String, Object>  getSprinklerGpsTrace(GpsClientStateQuery query){
        List<List<Map<String, Object>>> list = gpsClientStateDao.getGpsTraceSprinkler(query);

        Map<String, Object> res = new HashMap<>();
        res.put("Traces", list.get(1));
        res.put("Id", query.getObjId());
        if (list.get(2).size()>0){
            res.put("PlateNumber",list.get(2).get(0).get("c_name"));
            res.put("TotalDistance",list.get(2).get(0).get("c_total_distance"));
            res.put("TotalOilConsumption",list.get(2).get(0).get("c_total_oil_consumption"));
            res.put("InUseWaterRatio",list.get(2).get(0).get("c_in_section_use_water_ratio"));
            res.put("OutUseWaterRatio",list.get(2).get(0).get("c_out_section_use_water_ratio"));
            res.put("TotalTime", DateTimeUtil.GetTimeString(Integer.parseInt(list.get(2).get(0).get("c_total_seconds").toString())));
        }
        if(list.get(1).size()>0){
            LocalDateTime s = ((Timestamp)list.get(1).get(0).get("c_gps_date")).toLocalDateTime();
            LocalDateTime e = ((Timestamp)list.get(1).get(list.get(1).size()-1).get("c_gps_date")).toLocalDateTime();
            Map<String, Object> btp = new HashMap<>();
            btp.put("GpsDate",s);
            btp.put("Long",list.get(1).get(0).get("c_long"));
            btp.put("Lat",list.get(1).get(0).get("c_lat"));
            btp.put("AbsX",list.get(1).get(0).get("c_abs_x"));
            btp.put("AbsY",list.get(1).get(0).get("c_abs_y"));
            res.put("btp",btp);
            Map<String, Object> etp = new HashMap<>();
            etp.put("GpsDate",e);
            etp.put("Long",list.get(1).get(list.get(1).size()-1).get("c_long"));
            etp.put("Lat",list.get(1).get(list.get(1).size()-1).get("c_lat"));
            etp.put("AbsX",list.get(1).get(list.get(1).size()-1).get("c_abs_x"));
            etp.put("AbsY",list.get(1).get(list.get(1).size()-1).get("c_abs_y"));
            res.put("etp",etp);

        }
        return res;
    }

    /**
     * 根据自增id批量删除
     *
     * @param ids
     */
    public void deleteBatchById(String userId,Integer[] ids) {
        gpsClientStateDao.delete(userId, ids);
    }

    /**
     * 根据自增id保存或者更新
     *
     * @param gpsClientState 实体
     */
    public void saveOrUpdate(String userId, GpsClientState gpsClientState){
        gpsClientStateDao.saveOrUpdate(userId,gpsClientState);
    }
}
